//
//  GJCommonEmitter.m
//  xmgjAppFront
//
//  Created by ZekiMini on 2020/9/16.
//  Copyright © 2020 guojutech. All rights reserved.
//

#import "GJCommonEmitter.h"

@implementation GJCommonEmitter
RCT_EXPORT_MODULE()
- (NSArray<NSString *> *)supportedEvents{
  return @[@"RECORD_FILE_RESULT",@"VIDEO_PLAY_STATE",@"onScanFinishBlock",@"LOCATION_RESULT",@"MAP_ZOOM_LEVEL",@"map_position",@"FaceCheckHelper", @"WeChat_Resp",  @"waterFinish",@"USER_WATER_CAMERA", @"waterHistory", @"APP_NET_CONNECT"];
}



#pragma mark -- 给RN回调
/// 微信回调
/// @param parameter 本地地址
- (void)method_WeChat_Resp_withParameter:(NSDictionary *)parameter{
  [self sendEventWithName:@"WeChat_Resp" body:parameter];
}

/// 录像完成
/// @param parameter 本地地址
- (void)method_RECORD_FILE_RESULT_withParameter:(NSDictionary *)parameter{
  [self sendEventWithName:@"RECORD_FILE_RESULT" body:parameter[@"path"]];
}

/// 播放状态回调
/// @param parameter 回调状态
- (void)method_VIDEO_PLAY_STATE_withParameter:(NSDictionary *)parameter{
  [self sendEventWithName:@"VIDEO_PLAY_STATE" body:parameter[@"state"]];
}

- (void)method_onScanFinishBlock_withParameter:(NSDictionary *)parameter{
  [self sendEventWithName:@"onScanFinishBlock" body:parameter[@"qrcode"]];
}
- (void)method_LOCATION_RESULT_withParameter:(NSDictionary *)parameter{
  [self sendEventWithName:@"LOCATION_RESULT" body:[parameter  jsonStringEncoded]];
}
- (void)method_MAP_ZOOM_LEVEL_withParameter:(NSDictionary *)parameter{
  [self sendEventWithName:@"MAP_ZOOM_LEVEL" body:[parameter jsonStringEncoded]];
}

/// 地图选点回调
/// @param parameter 回调参数
- (void)method_map_position_withParameter:(NSDictionary *)parameter{
  [self sendEventWithName:@"map_position" body:[parameter jsonStringEncoded]];
}

- (void)method_FaceCheckHelper_withParameter:(NSDictionary *)parameter{
  [self sendEventWithName:@"FaceCheckHelper" body:[parameter jsonStringEncoded]];
}


- (void)method_waterFinish_withParameter:(NSDictionary *)parameter{
  [self sendEventWithName:@"waterFinish" body:[parameter jsonStringEncoded]];
}


- (void)method_USER_WATER_CAMERA_withParameter:(NSDictionary *)parameter{
  [self sendEventWithName:@"USER_WATER_CAMERA" body:[parameter jsonStringEncoded]];
}

- (void)method_waterHistory_withParameter:(NSArray *)parameter{
  [self sendEventWithName:@"waterHistory" body:[parameter jsonStringEncoded]];
}
- (void)method_APP_NET_CONNECT_withParameter:(NSDictionary *)parameter{
  [self sendEventWithName:@"APP_NET_CONNECT" body:[parameter jsonStringEncoded]];
}



#pragma mark -- initlization && 转换方法

- (void)commonEmitterNotificationAction:(NSNotification *)notification{
  NSDictionary *userInfo = notification.userInfo;
  NSString *actionKey = userInfo[@"actionKey"];
  id dataDic = userInfo[@"parameter"];
  if ([actionKey gj_hasValue]) {
    NSString *methodName = [NSString stringWithFormat:@"method_%@_withParameter:",actionKey];
    [self doAction:methodName andArgObject:dataDic];
  }
}


-(void) doAction:(NSString *)methodsName andArgObject:(id)objectArg
{
  SEL selector = NSSelectorFromString(methodsName);
  if (self)
  {
    if ([self respondsToSelector:selector])
    {
      [self performSelectorOnMainThread:selector withObject:objectArg waitUntilDone:NO];
    }
  }
}

- (instancetype)init
{
  self = [super init];
  if (self) {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(commonEmitterNotificationAction:) name:GJKGJCommonEmitterNotification object:nil];
  }
  return self;
}
- (void)dealloc
{
  
  [[NSNotificationCenter defaultCenter] removeObserver:self name:GJKGJCommonEmitterNotification object:nil];
  
}


@end
